from openai import OpenAI
from ..settings.settings import Settings
from fastapi import APIRouter
from fastapi.responses import JSONResponse
from langchain_core.prompts import ChatPromptTemplate

router = APIRouter()

settings = Settings()  # 实例化
DeepSeekUrl =  settings.DeepSeekUrl
DeepSeekKey = settings.DeepSeekKey
# 初始化客户端
client = OpenAI(api_key=DeepSeekKey,base_url=DeepSeekUrl)
# 定义提示词模板
prompt = ChatPromptTemplate.from_template("{question}")


@router.get("/deepseek_test")
async def deepseek_test():
    try:
        # 使用提示词模板生成最终提示
        formatted_prompt = prompt.format(question="请以表格的形式返回三国演义实力最强的十个人，并进行简要介绍")
        # 调用 DeepSeek
        response = client.chat.completions.create(
            model="deepseek-chat",
            messages=[
                {"role": "system", "content": "You are a helpful assistant"},
                {"role": "user", "content": formatted_prompt},  # 用模板生成的内容
            ],
            stream=False
        )
        result = response.choices[0].message.content
        return JSONResponse(content={"message": result})
    except Exception as e:
        return JSONResponse(content={"error": str(e)}, status_code=500)